package com.qianfeng.project.fragment;

import com.qianfeng.project.R;
import com.qianfeng.project.global.AppConstants;
import com.qianfeng.project.global.BaseApplication;
import com.qianfeng.project.global.BaseFragment;
import com.qianfeng.project.model.webservice.UrlConstants;
import com.qianfeng.tools.MyToast;
import com.qianfeng.tools.Utils;
import com.qianfeng.tools.multidownload.GetServerVersionXml;

import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.widget.Toast;

public class MemberFragment extends BaseFragment implements OnClickListener {
	// 个人中心各个信息栏(一条信息)
	private ImageView iv_zoom;
	private LinearLayout linear_member_account;
	private LinearLayout linear_member_order;
	private LinearLayout linear_member_share;
	private LinearLayout linear_member_log_out;
	private LinearLayout linear_member_about_us;
	private LinearLayout linear_member_clear_cache;
	private LinearLayout linear_member_collect;
	private LinearLayout linear_member_coupons;// 优惠券
	private LinearLayout linear_member_bills;// 发票

	private RelativeLayout relative_update;
	private TextView tv_loginStatus, cacheData, tv_userName;
	private String cacheSize = "0kb";
	// 有更新
	private ImageView can_update;
	private ScrollView scroll_view;
	private ScrollView scroll_inner;

	// 存储从服务器解析的XML数据
	private TextView already_new; // 已是最新版本
	private String SDUrl;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		// 填充 MemberFragment 的布局
		View view = inflater.inflate(R.layout.fragment_member, null, false);
		scroll_view = (ScrollView) view.findViewById(R.id.scroll_view);

		// 个人中心信息---每一条文字内容布局
		View contentView = LayoutInflater.from(getActivity()).inflate(R.layout.frag_personal, null, false);
		scroll_view.addView(contentView);

		scroll_inner = (ScrollView) contentView.findViewById(R.id.scroll_inner);

		iv_zoom = (ImageView) scroll_inner.findViewById(R.id.iv_zoom);
		linear_member_account = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_account);
		linear_member_order = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_order);
		linear_member_share = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_share);
		linear_member_log_out = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_log_out);
		linear_member_about_us = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_about_us);
		linear_member_clear_cache = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_clear_cache);
		linear_member_collect = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_collect);
		linear_member_coupons = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_coupons);
		linear_member_bills = (LinearLayout) scroll_inner.findViewById(R.id.linear_member_bills);

		tv_userName = (TextView) scroll_inner.findViewById(R.id.tv_userName);
		can_update = (ImageView) scroll_inner.findViewById(R.id.can_update);
		cacheData = (TextView) scroll_inner.findViewById(R.id.cacheData);
		already_new = (TextView) scroll_inner.findViewById(R.id.already_new);
		tv_loginStatus.setText("未登录");
		relative_update = (RelativeLayout) scroll_inner.findViewById(R.id.linear_member_update);
		tv_loginStatus = (TextView) scroll_inner.findViewById(R.id.tv_loginStatus);

		// 注册监听
		linear_member_share.setOnClickListener(this);
		linear_member_account.setOnClickListener(this);
		linear_member_order.setOnClickListener(this);
		linear_member_clear_cache.setOnClickListener(this);
		linear_member_log_out.setOnClickListener(this);
		linear_member_collect.setOnClickListener(this);
		linear_member_about_us.setOnClickListener(this);
		relative_update.setOnClickListener(this);
		linear_member_bills.setOnClickListener(this);
		linear_member_coupons.setOnClickListener(this);
		relative_update.setClickable(false);

		// 检查网络连接
		if (Utils.checkNetworkConnection(this.getActivity())) {
			if (BaseApplication.serCode == 0) {
				// 获取版本号
				getVersionCode(this.getActivity());
				new GetServerVersionXml(relative_update, can_update, already_new).execute(UrlConstants.VERSIONURL);
			} else {
				// 通过本地版本与服务器版本的比较判断是否可以更新,以及跟新条目是否可以被点击
				if (BaseApplication.serCode > BaseApplication.locCode) {
					can_update.setVisibility(View.VISIBLE);
					already_new.setVisibility(View.GONE);
					relative_update.setClickable(true);
				} else {
					can_update.setVisibility(View.GONE);
					already_new.setVisibility(View.VISIBLE);
					relative_update.setClickable(false);
				}
			}
		}else {
			Toast.makeText(getActivity(), "网络连接异常", Toast.LENGTH_SHORT).show();
		}
		
		//检测SD卡
		if (Utils.isSdCardExist()) {
			if (getActivity().getExternalCacheDir().getAbsolutePath() != null) {
				SDUrl = this.getActivity().getExternalCacheDir().getAbsolutePath();
			}else {
				MyToast.shortToast(getActivity(), "没有检测到SD卡！");
			}
		} else {
			MyToast.shortToast(getActivity(), "没有检测到SD卡！");
		}
		return view;
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		//显示目前已有的缓冲
		BaseApplication.isLogin = BaseApplication.sp.getBoolean(AppConstants.ISLOGIN, false);
		BaseApplication.username = BaseApplication.sp.getString(AppConstants.USERNAME, null);
		
		
	}
	
	// 获取软件版本号
	public void getVersionCode(Context context) {
		int localCode = 0;
		String localName = "";
		try {
			PackageManager packageManager = context.getPackageManager();
			PackageInfo packageInfo = packageManager.getPackageInfo(context.getPackageName(), 0);
			// 获取软件版本号,对应AndroidManifest.xml下android:versionCode
			localCode = packageInfo.versionCode;
			localName = packageInfo.versionName;
			BaseApplication.locCode = localCode;
			BaseApplication.locName = localName;

		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}

}
